package com.njit;

/**
 * Created by ospip on 2017/11/16.
 * Copyright (c) 2017 LLAP. All rights reserved.
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class RegisterFrame extends JFrame {

    private JTextField username;
    private JPasswordField password, retype;
    private JRadioButton sexFemale, sexMale;
    private JPanel sex, birth, fav;
    private JTextField year;
    private JComboBox month, day;
    private JCheckBox f1, f2, f3;
    private JButton register, cancel;
    private JTextArea remmond;
    private JScrollPane scroll;

    public RegisterFrame() {
        super("Register");
        this.setSize(450, 400);
        this.setLocationRelativeTo(getOwner());
        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // Begin

        JPanel cont = new JPanel(new GridLayout(6, 2));
        cont.add(new JLabel("Username"));
        username = new JTextField(10);
        cont.add(username);
        cont.add(new JLabel("Password"));
        password = new JPasswordField(10);
        cont.add(password);
        cont.add(new JLabel("Retype password"));
        retype = new JPasswordField(10);
        cont.add(retype);
        cont.add(new JLabel("Gender"));
        sexMale = new JRadioButton("M");
        sexFemale = new JRadioButton("F");
        ButtonGroup bg = new ButtonGroup();
        bg.add(sexFemale); bg.add(sexMale);
        sex = new JPanel(new GridLayout(1, 2));
        sex.add(sexMale); sex.add(sexFemale);
        cont.add(sex);
        cont.add(new JLabel("Birthday"));
        year = new JTextField(4);
        month = new JComboBox();
        for (int i = 1; i <= 12; i++) {
            month.addItem(i);
        }
        day = new JComboBox();
        for (int i = 1; i <= 31; i++) {
            day.addItem(i);
        }
        birth = new JPanel();
        birth.add(year);
        birth.add(new JLabel("-"));
        birth.add(month);
        birth.add(new JLabel("-"));
        birth.add(day);
        cont.add(birth);
        cont.add(new JLabel("Hobbies"));
        f1 = new JCheckBox("Sports");
        f2 = new JCheckBox("Movies");
        f3 = new JCheckBox("Music");
        fav = new JPanel();
        fav.add(f1); fav.add(f2); fav.add(f3);
        cont.add(fav);

        JPanel cont1 = new JPanel(new GridLayout(1, 2));
        cont1.add(new JLabel("Resume"));
        remmond = new JTextArea(5, 10);
        scroll = new JScrollPane(remmond);
        cont1.add(scroll);

        JPanel cont2 = new JPanel(new GridLayout(1, 2));
        register = new JButton("Register");
        register.addActionListener((e) -> {
            String pass = new String(password.getPassword());
            String repass = new String(retype.getPassword());
            if (pass.equals(repass)) {
                String s;
                s = "Username: " + username.getText() + "\n";
                s += "Password: " + pass + "\n";
                s += "Gender: " + (sexMale.isSelected() ? sexMale.getText() : sexFemale.getText()) + "\n";
                s += "Birthday: " + String.format("%s-%s-%s\n",
                        year.getText(), month.getSelectedItem(), day.getSelectedItem());
                s += "Hobbies: " +
                        (f1.isSelected() ? f1.getText() : "") +
                        (f2.isSelected() ? f2.getText() : "") +
                        (f3.isSelected() ? f3.getText() : "") + "\n";
                s += "Resume: " + remmond.getText();
                JOptionPane.showMessageDialog(null,  s);
            }
            else {
                JOptionPane.showMessageDialog(null, "Passwords do not match");
            }
        });
        cancel = new JButton("Cancel");
        cancel.addActionListener((e) -> {
            dispose();
        });
        cont2.add(register);
        cont2.add(cancel);

        this.add(cont);
        this.add(cont1);
        this.add(cont2);
        // End

    }

    public static void main(String[] args) {
        new RegisterFrame().setVisible(true);
    }
}